package alf.mapping.view.activity;

import alf.mapping.view.activity.R;
import alf.mapping.view.visuals.EditorEngine;
import alf.mapping.view.visuals.ThemeManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.FrameLayout;

public class ImageSelectionActivity extends Activity {

	private String[] types;
	private EditorEngine editor;
	private String selectedName;
	
	private static final String GROUP_PARAM = "type";
	private static final String DEFAULT_PARAM = "default";

	public static Uri prepareCall(String imageType, String defaultValue) {
		Uri.Builder b = new Uri.Builder().appendPath("themeImage");
		
		b.appendQueryParameter(GROUP_PARAM, imageType);
		b.appendQueryParameter(DEFAULT_PARAM, defaultValue);
		return b.build();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.image_selection);

		editor = EditorEngine.getInstance();
		Uri data = getIntent().getData();
		String imageType = data.getQueryParameter(GROUP_PARAM);
		types = editor.getTheme().getAvaliableImages(imageType);

		GridView grid = (GridView) this.findViewById(R.id.image_grid);
		ImageArrayAdapter adapter = new ImageArrayAdapter(this);
		adapter.setData(imageType,types);
		grid.setAdapter(adapter);

		grid.setOnItemClickListener(new GridView.OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
					selecciona(position);
				}
			});
	}

	private void selecciona(int position) {
		selectedName = types[position];
		returnResult();
	}

	private void returnResult() {
		Intent i = getIntent();
		i.putExtra(Intent.EXTRA_RETURN_RESULT, selectedName);
		setResult(RESULT_OK, i);
		finish();
	}

	// from java samples...
	class ImageArrayAdapter extends BaseAdapter {
		Context mContext;
		private String[] names;
		private String group;

		public void setData(String group,String[] avaliableImages) {
			this.group = group;
			this.names = avaliableImages;
		}

		public int getCount() {
			if (names != null) {
				return names.length;
			}
			return 0;
		}

		public Object getItem(int p1) {
			if (names != null) {
				return names[p1];
			}
			return null;
		}

		public long getItemId(int p1) {
			// TODO: Implement this method
			return -1;
		}

		public View getView(int pos, View convertView, ViewGroup parent) {
			ImageView imageView;
			if (convertView == null) {  // if it's not recycled, initialize some attributes
				imageView = new ImageView(mContext);
				imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imageView.setPadding(8, 8, 8, 8);
			} else {
				imageView =  (ImageView) convertView;

			}

			Bitmap bitmap = editor.getBitmap(this.group,names[pos]);
			imageView.setImageBitmap(bitmap);
			return imageView;
		}

		public ImageArrayAdapter(Context c) {
			super();
			this.mContext = c;
		}

	}

}
